var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_show_time: 1,
    currentNav: 'newest', // 添加当前导航状态，默认选中最新
    sort: 'desc', // 排序方式
    order: 'common_sell_time' // 排序字段
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // 默认按最新排序（发布日期降序）
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&order=common_sell_time&sort=desc&typ=json',
      method: 'GET',
      data: {
        id: options.shop_id,
        type_wxapp: 'wxapp'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items
        that.setData({
          goods: good_info,
          shop_id: options.shop_id,
          is_show_time: 0,
          currentNav: 'newest'
        })
      }
    })
  },

  // 导航切换
  switchNav: function(e) {
    const nav = e.currentTarget.dataset.nav;
    this.setData({
      currentNav: nav
    });
  },

  // 最新排序（按发布日期）
  newest: function() {
    var that = this
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&order=common_sell_time&sort=desc&typ=json',
      method: 'GET',
      data: {
        id: that.data.shop_id,
        type_wxapp: 'wxapp'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items
        that.setData({
          goods: good_info,
          currentNav: 'newest',
          order: 'common_sell_time',
          sort: 'desc'
        })
      }
    })
  },

  // 销量排序
  salenum: function() {
    var that = this
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&order=common_salenum&sort=desc&typ=json',
      method: 'GET',
      data: {
        id: that.data.shop_id,
        type_wxapp: 'wxapp'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items
        that.setData({
          goods: good_info,
          currentNav: 'salenum',
          order: 'common_salenum',
          sort: 'desc'
        })
      }
    })
  },

  // 价格排序
  price: function() {
    var that = this
    var sorts = that.data.sort
    var newSort = sorts === 'desc' ? 'asc' : 'desc'
    
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&order=common_price&sort=' + newSort + '&typ=json',
      method: 'GET',
      data: {
        id: that.data.shop_id,
        type_wxapp: 'wxapp'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items
        that.setData({
          goods: good_info,
          currentNav: 'price',
          order: 'common_price',
          sort: newSort
        })
      }
    })
  },

  //跳转至全部商品
  all: function (e) {
    this.setData({
      currentNav: 'all'
    });
    wx.redirectTo({
      url: '../store_goods/store_goods?shop_id=' + this.data.shop_id
    })
  },

  //跳转店铺首页
  store: function (e) {
    this.setData({
      currentNav: 'store'
    });
    wx.redirectTo({
      url: '../store/store?shop_id=' + this.data.shop_id
    })
  },

  search: function () {
    wx.redirectTo({
      url: '../search_store/search_store?shop_id=' + this.data.shop_id
    })
  }
})